<?php
/**
 * Created by JetBrains PhpStorm.
 * User: h2a!
 * Date: 21/11/2012
 * Time: 23:46
 * To change this template use File | Settings | File Templates.
 */

class Manage_Product_Controller extends CI_Controller {
    function get_all() {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load->model('admin/manage_product_model');
            $data['query'] = $this->manage_catalog_model->get_all_catalogs();
            $this->load->view('admin/product_view', $data);
        }
    }

    function insert_product() {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            //load library, view and model
            $this->load-> model('admin/manage_product_model');
            $this->load->view('admin/product_insertion');
            //create values array to insert to database
            $product_id = $this->input->post('masp');
            $product_name = $this->input->post('tensp');
            $product_price_origin = $this->input->post('giagoc');
            $product_price = $this->input->post('giaban');
            $category_id = $this->input->post('catalog');
            $data = array(
                'product_id' => $product_id,
                'product_name' => $product_name,
                'product_price_origin' => $product_price_origin,
                'product_price' => $product_price,
                'category_id' => $category_id
            );
            //if user clicks submit button, call to manage_product_model model to insert to database
            if($this->input->post('gui')) {
                $this->manage_product_model->insert_product($data);
            }
        }
    }

    public function edit_product($id = 0) {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load-> model('admin/manage_product_model');
            $query = $this->manage_product_model->get_product($id);
            $data['ftitle']['value'] = $query['product_id'];
            $this->load->view('admin/product_edit', $data);
            $product_name = $this->input->post('tensp');
            $product_price_origin = $this->input->post('giagoc');
            $product_price = $this->input->post('giaban');
            $category_id = $this->input->post('catalog');
            $data2 = array(
                'product_name' => $product_name,
                'product_price_origin' => $product_price_origin,
                'product_price' => $product_price,
                'category_id' => $category_id
            );

            if($this->input->post('edit')) {
                $this->manage_product_model->edit_product($data2, $id);
            }
        }
    }

    public function delete_product($id = 0) {
        if(!$this->session->userdata('logged_in')) {
            $this->load->view('login_view');
        } else {
            $this->load-> model('admin/manage_product_model');
            $this->manage_product_model->delete_product($id);
        }
    }
}

?>